package main

import (
	"flag"
	"github.com/seafooler/dumbo/config"
	"github.com/seafooler/dumbo/core"
	"time"
)

var conf *config.Config
var err error

func init() {
	configFile := flag.String("config", "config", "Configuration file name (without extension)")
	flag.Parse()
	
	conf, err = config.LoadConfig("", *configFile)
	if err != nil {
		panic(err)
	}
}

func main() {
	// Create and initialize node
	node := core.NewNode(conf)
	
	// Start P2P listening
	if err = node.StartP2PListen(); err != nil {
		panic(err)
	}
	
	// Wait for other nodes to start
	time.Sleep(time.Second * time.Duration(conf.WaitTime))
	
	// Establish connections
	if err = node.EstablishP2PConns(); err != nil {
		panic(err)
	}
	
	// Start consensus
	node.StartConsensus()
	
	// Keep the main thread alive
	for {
		time.Sleep(time.Second)
	}
}